package controllers

import (
	"git.oschina.net/goxiang2/web/admin/models"
	"github.com/astaxie/beego"
)

type MainController struct {
	beego.Controller
}

type IndexInfo struct {
	Id   int
	Name string
	Url  string
}

func getIndex(permission int64) map[interface{}]interface{} {
	data := make(map[interface{}]interface{})
	if (permission & (1 << uint(models.ModelUser))) != 0 {
		data["system_mgr"] = []*IndexInfo{
			{
				Id:   1,
				Name: "用户管理",
				Url:  "user",
			},
			{
				Id:   2,
				Name: "角色管理",
				Url:  "role",
			},
			{
				Id:   3,
				Name: "日志管理",
				Url:  "log",
			},
		}
	}
	return data
}

func (c *MainController) Get() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/login")
		return
	}
	c.Data["title"] = beego.AppConfig.String("apptitle")
	c.Data["username"] = userinfo.(*LoginInfo).Username
	user_permission := models.GetPermission(userinfo.(*LoginInfo).Username)
	data := getIndex(user_permission)
	for k, v := range data {
		c.Data[k] = v
	}
	c.TplName = "index.ace"
}
